package paylayer

import (
	"strconv"
	"strings"
	"dhfshop/models/misc"
	"encoding/json"
	"dhfshop/models/isv"
	"dhfshop/utils/math"
	"dhfshop/models/shopping"
	"github.com/astaxie/beego"
	"thirdpaysdk/api/usagms"
	"dhfshop/service/redisgo"
	"github.com/pkg/errors"
)


func OrderUsagms(appId,openId string,acPay *isv.AcPayParams,order *shopping.ValueOrder)(rsp PayResponse,prepayId string, err error) {
	
	partnerCode := acPay.MchId
	credentialCode := acPay.Appkey
	currency := acPay.MediaId
	orderStr := strconv.FormatInt(order.OrderId,10)
	des := "订单" + "[" + orderStr + "]"
	operator := ""
	royPayNotify := beego.AppConfig.String("WXPayNotify") + "/public/usagms/notify"
	royPayRsp, err := usagms.MicroAppPay(partnerCode,orderStr,credentialCode,des,currency,
										royPayNotify,operator,appId,openId,int(math.MultiTotalPriceInt(order.Payment, 100)))
	if err != nil {
		err = errors.Wrap(err, "MicroAppPay... ")
		beego.Debug(err.Error())
		return 
	}
	prepayId = strings.Replace(royPayRsp.SdkParams.Package, "prepay_id=", "", -1)
	
	//更新订单支付方式编号
	order.PayWayCode = 12
	shopping.UpdateOrderPayWayCode(order)
	payRsp := misc.WxResponse{}
	payRsp.AppId = royPayRsp.SdkParams.AppId
	payRsp.NonceStr = royPayRsp.SdkParams.NonceStr
	payRsp.Package = royPayRsp.SdkParams.Package
	payRsp.SignType = royPayRsp.SdkParams.SignType
	payRsp.TimeStamp = royPayRsp.SdkParams.TimeStamp
	payRsp.PaySign = royPayRsp.SdkParams.PaySign
	if tempJson, err := json.Marshal(payRsp); err == nil { 
	   rsp.WxpayRsp = string(tempJson)
	}
	redisconn := redisgo.GetInstance()
	cacheKey := "payorder_" + orderStr
	var payInfoCache *PayInfoCache = new(PayInfoCache)
	payInfoCache.AppId = appId
	payInfoCache.MchId = acPay.MchId
	payInfoCache.AppKey = acPay.Appkey
	payInfoCache.OrderKind = "ESHOP_ORDER_PAY"
	if payInfoJSON, err := json.Marshal(payInfoCache); err == nil { 
		if _, err = redisconn.Set(cacheKey, string(payInfoJSON), 72*60*60); err != nil {  
			beego.Debug("SETEX", err.Error())
		}
	}
	return
}

func ChargeUsagms(appId,openId,outTradeNo string,acPay *isv.AcPayParams,amount float32)(rsp PayResponse,prepayId string,err error){
    partnerCode := acPay.MchId
	credentialCode := acPay.Appkey
	currency := acPay.MediaId
	des := "账户充值" + "[" + outTradeNo + "]"
	operator := ""
	royPayNotify := beego.AppConfig.String("WXPayNotify") + "/public/usagms/notify"
	royPayRsp, err := usagms.MicroAppPay(partnerCode,outTradeNo,credentialCode,des,currency,
										royPayNotify,operator,appId,openId,int(math.MultiTotalPriceInt(amount, 100)))
	if err != nil {
		err = errors.Wrap(err, "MicroAppPay... ")
		beego.Debug(err.Error())
		return 
	}
	prepayId = strings.Replace(royPayRsp.SdkParams.Package, "prepay_id=", "", -1)
	payRsp := misc.WxResponse{}
	payRsp.AppId = royPayRsp.SdkParams.AppId
	payRsp.NonceStr = royPayRsp.SdkParams.NonceStr
	payRsp.Package = royPayRsp.SdkParams.Package
	payRsp.SignType = royPayRsp.SdkParams.SignType
	payRsp.TimeStamp = royPayRsp.SdkParams.TimeStamp
	payRsp.PaySign = royPayRsp.SdkParams.PaySign
	if tempJson, err := json.Marshal(payRsp); err == nil {
		rsp.WxpayRsp = string(tempJson)
	}
	redisconn := redisgo.GetInstance()
	cacheKey := "payorder_" + outTradeNo
	var payInfoCache *PayInfoCache = new(PayInfoCache)
	payInfoCache.AppId = appId
	payInfoCache.MchId = acPay.MchId
	payInfoCache.AppKey = acPay.Appkey
	payInfoCache.OrderKind = "MEMBER_CHARGE"
	if payInfoJSON, err := json.Marshal(payInfoCache); err == nil { 
		if _, err = redisconn.Set(cacheKey, string(payInfoJSON), 72*60*60); err != nil {  
			beego.Debug("SETEX", err.Error())
		}
	}
	return
}

func CommonUsagms(appId,openId,orderId,body,kind string,acPay *isv.AcPayParams,amount float32)(rsp PayResponse,prepayId string, err error){
    partnerCode := acPay.MchId
	credentialCode := acPay.Appkey
	currency := acPay.MediaId
	operator := ""
	royPayNotify := beego.AppConfig.String("WXPayNotify") + "/public/usagms/notify"
	royPayRsp, err := usagms.MicroAppPay(partnerCode,orderId,credentialCode,body,currency,
										royPayNotify,operator,acPay.AppId,openId,int(math.MultiTotalPriceInt(amount, 100)))
	if err != nil {
		err = errors.Wrap(err, "MicroAppPay... ")
		beego.Debug(err.Error())
		return 
	}
	prepayId = strings.Replace(royPayRsp.SdkParams.Package, "prepay_id=", "", -1)
	payRsp := misc.WxResponse{}
	payRsp.AppId = royPayRsp.SdkParams.AppId
	payRsp.NonceStr = royPayRsp.SdkParams.NonceStr
	payRsp.Package = royPayRsp.SdkParams.Package
	payRsp.SignType = royPayRsp.SdkParams.SignType
	payRsp.TimeStamp = royPayRsp.SdkParams.TimeStamp
	payRsp.PaySign = royPayRsp.SdkParams.PaySign
	if tempJson, err := json.Marshal(payRsp); err == nil { 
	   rsp.WxpayRsp = string(tempJson)
	}
	redisconn := redisgo.GetInstance()
	cacheKey := "payorder_" + orderId
	var payInfoCache *PayInfoCache = new(PayInfoCache)
	payInfoCache.AppId = appId
	payInfoCache.MchId = acPay.MchId
	payInfoCache.AppKey = acPay.Appkey
	payInfoCache.OrderKind = kind
	if payInfoJSON, err := json.Marshal(payInfoCache); err == nil { 
		if _, err = redisconn.Set(cacheKey, string(payInfoJSON), 72*60*60); err != nil {  
			beego.Debug("SETEX", err.Error())
		}
	}
	return
}
